﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WuxueInfoPanel : MonoBehaviour {

    private Text NameText;
    private Text InfoText;
    private PersonageWuxue PersonageWuxue;

	// Use this for initialization
	void Start () {
        this.NameText = transform.Find("Name").GetComponent<Text>();
        this.InfoText = transform.Find("Info").GetComponent<Text>();
        Personage player = GlobalData.Instance.GetPlayerPersonage();
        this.Refresh(player.OwnWuxues);
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    /// <summary>
    /// 刷新武学信息
    /// </summary>
    /// <param name="personageWuxues"></param>
    public void Refresh(List<PersonageWuxue> personageWuxues) {
        if (personageWuxues == null || personageWuxues.Count == 0) {
            this.Refresh((PersonageWuxue) null);
        } else {
            this.Refresh(personageWuxues[0]);
        }
    }

    /// <summary>
    /// 刷新武学信息
    /// </summary>
    /// <param name="personageWuxue"></param>
    public void Refresh(PersonageWuxue personageWuxue) {
        if (!this.IsEqual(personageWuxue, this.PersonageWuxue)) {
            this.PersonageWuxue = personageWuxue;
            this.UpdateUI();
        }
    }

    /// <summary>
    /// 更新ui
    /// </summary>
    private void UpdateUI() {
        if (this.PersonageWuxue == null) {
            this.NameText.text = "";
            this.InfoText.text = "";
        } else {
            this.NameText.text = this.PersonageWuxue.Wuxue.Name;
            this.InfoText.text = this.PersonageWuxue.Wuxue.Intro;
        }
    }

    /// <summary>
    /// 武学是否相同
    /// </summary>
    /// <param name="personageWuxue1"></param>
    /// <param name="personageWuxue2"></param>
    /// <returns></returns>
    private bool IsEqual(PersonageWuxue personageWuxue1, PersonageWuxue personageWuxue2) {
        if (personageWuxue1 == null || personageWuxue2 == null) {
            return false;
        } else {
            return personageWuxue1.WuxueId == personageWuxue2.WuxueId && personageWuxue1.Grade == personageWuxue2.Grade;
        }
    }
}
